package xyz.naoyu.servlets.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import xyz.naoyu.dao.BaseDao;
import xyz.naoyu.entity.Item;

import xyz.naoyu.servlets.service.CurdService;
import xyz.naoyu.settings;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yWieLoc
 * @date 2022/3/18 22:03
 */

@WebServlet("/api/item")
public class ItemServlet extends BaseServlet implements CurdService {

    @Override
    public void del(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Item item = new Item(req.getParameter("Id"));

        try {
            if(item.delSelf(tools)) {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(true,"删除成功", null)
                ));
            }else  {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"删除失败", null)
                ));
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Item item = new Item(req.getParameter("Id"));

        try {
            // 获取后端信息
            item = (Item) item.getInfo(tools);

            // 新建
            if(item.getCreatorId() == null ) {
                item = JSONObject.parseObject(req.getParameter("item"),item.getClass());


                item.setCreatorId((String) req.getSession().getAttribute("Id"));
                item.setCreateTime(String.valueOf(System.currentTimeMillis()));



            } else {

                // 认证修改
                if(item.getHoldId() != req.getSession().getAttribute("Id")) {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(false,"没有权限",null)
                    ));
                    return;
                }
                // 加载参数
                item = (Item) JSON.parse(req.getParameter("item"));


            }

            item.setCreatorId(item.getId());
            item.setType("Item");
            // 传向后端
            if(item.infoUpdate(tools) == 0) {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"更新失败",null)
                ));
                return;
            } else {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(true,"更新成功",null)
                ));
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("编辑失败");
        }
    }

    @Override
    public void show(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();      // 连接工具
        Item item = new Item(req.getParameter("Id"));

        try {
            // 后端数据
            item = (Item) item.getInfo(tools);

            // 无数据或被锁定
            if(item.getState() == null  || item.getState().equals(settings.state.getString("LOCK")))  {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"未找到信息",null)
                ));
                return;
            } else {

                // TODO: 2022/3/19 权限判定
                // 除去关键信息
                item.InfoLimit();
//                System.out.println(JSON.toJSONString(item));
                // 打包发送
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(true,null, JSON.toJSONString(item))
                ));
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("item 请求信息出错");
        }

    }

    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) {
        CurdService.super.list(req, resp);
    }
}
